/**
 * Dear programmer:
 * When I wrote this code, only god and I knew how it worked.
 * Now, only god knows it!
 *
 * @author: Pegasus  Date: 2018-12-15 Time: 23:16
 *
 * 新建报修工单
 */

import React, {Component} from 'react';
import {
    SafeAreaView,
    View,
    Text,
    TouchableOpacity,
    TextInput,
} from 'react-native';
import {connect} from 'react-redux';
import {styles} from './InputReasonScreen.Style';
import {COLOR_WHITE, FONT_COLOR_SUB_TITLE, FONT_SIZE_TITLE } from "../../../../../../constant/PHColors"
import {orderChangeState} from "./Actions"
import {FetchStatus} from "../../../../../../configuration/RequestConfiguration";
import {ReturnStatus} from "../../../../../../configuration/ReturnStatus";
import {doDisplaySuccessMessage, doDisplayWarnMessage} from "../../../../../../component/Message/Action";
import {QmsRecordSummary} from "../../../../JobScreen/Actions";

class InputReasonScreen extends Component {

    static navigationOptions = ({navigation}) => ({
        title: navigation.getParam('operationName'),
        headerRight: (
            <TouchableOpacity activeOpacity={0.9} onPress={navigation.getParam('submitClick')}>
                <View style={{ height: 44,paddingRight:15, flexDirection: 'row', justifyContent: 'flex-end', alignItems: 'center'}}>
                    <Text style={{fontSize: 18, color: '#212121'}}>
                        完成
                    </Text>
                </View>
            </TouchableOpacity>
        )
    });

    //构造
    constructor(props) {
        super(props);
        this.state = {
            remark:'',
        }
    }

    componentWillReceiveProps(nextProps, nextContext): void {
        let {dispatch} = this.props;

        if (nextProps.onOrderChangeStateMsg !== this.props.onOrderChangeStateMsg && nextProps.onOrderChangeStateMsg.fetchStatus === FetchStatus.Success) {
            if (nextProps.onOrderChangeStateMsg.RETURN === ReturnStatus.Success) {
                doDisplaySuccessMessage(nextProps.onOrderChangeStateMsg.MSG);

                let str = {};
                dispatch(QmsRecordSummary(JSON.stringify(str)));

                this.props.navigation.state.params.goBack();
            } else {
                dispatch(doDisplayWarnMessage(nextProps.onOrderChangeStateMsg.MSG))
            }
        }
    }

    componentDidMount(): void {

    }

    componentWillMount() {
        this.props.navigation.setParams({submitClick: this._submitClick});
    }

    render(){
        return (
            <SafeAreaView style={styles.body}>
                <View style={styles.bodyTitle}>
                    <Text style={styles.bodyTitleText}>
                        请输入{this.props.navigation.getParam('operationName')}原因：
                    </Text>
                </View>
                {/* 填写原因 */}
                <View style={styles.bodyContainer}>
                    <View style={styles.inputBox}>
                        <TextInput
                            multiline={true}
                            maxLength={50}
                            style={{
                                fontSize: 20,
                                padding: 0,
                                height:150,
                                textAlignVertical: "top",
                            }}
                            keyboardType={'default'}
                            autoFocus={true}
                            underlineColorAndroid={'transparent'}
                            onChangeText={text => {
                                if(text.length>50){
                                    text = text.substring(0,50)
                                }
                                this.setState({
                                    remark:text,
                                })
                            }}
                        />
                        <View style={styles.textInputRight}>
                            <Text style={{fontSize: FONT_SIZE_TITLE, color: FONT_COLOR_SUB_TITLE}}>
                                {this.state.remark.length}
                                <Text style={{fontSize: FONT_SIZE_TITLE, color: FONT_COLOR_SUB_TITLE}}>
                                    /50
                                </Text>
                            </Text>
                        </View>
                    </View>
                </View>
            </SafeAreaView>
        )
    }

    _submitClick=()=>{
        let {dispatch} = this.props;
        let str = {"qms_repair":{"sn":this.props.navigation.getParam('sn'),"status_remark":this.state.remark}};
        let procedure = this.props.navigation.getParam("procedure");
        dispatch(orderChangeState(JSON.stringify(str),procedure));
    }

}

const props = state => {
    return {
        onOrderChangeStateMsg:state.onOrderChangeStateMsg
    }
};

export default InputReasonScreen = connect(props)(InputReasonScreen);
